package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐及其对应的菜品关系数据
     *
     * @param setmealDTO
     * @return
     */
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //向套餐表插入数据
        setmealMapper.insert(setmeal);

        //获取自增id
        Long setmealId = setmeal.getId();

        //向套餐菜品关系表插入多条数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //判断是否有菜品数据，并填充套餐id
        if (setmealDishes != null && setmealDishes.size() > 0) {
            //把每个菜品添加套餐id
            setmealDishes.forEach(dish -> dish.setSetmealId(setmealId));
            //批量插入 菜品关系
            setmealDishMapper.insertBatch(setmealDishes);
        }

    }

    /**
     * 套餐的分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.getPage(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @Transactional
    public void delete(List<Long> ids) {
        //起售中的套餐不能删除  判断
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id); //查询套餐信息
            if (setmeal.getStatus().equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除套餐表中对应的数据
        setmealMapper.deleteByIds(ids);
        //删除套餐关系中对应的数据
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 修改套餐数据
     *
     * @param setmealDTO
     * @return
     */
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //修改套餐的基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        setmealMapper.update(setmeal);

        //删除套餐关系表中对应的数据
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());

        //然后再添加新的套餐关系表中对应的数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        setmealDishes.forEach(setmealDish ->
                setmealDish.setSetmealId(setmealDTO.getId()));

        //（批量插入）
        setmealDishMapper.insertBatch(setmealDishes);
    }


    /**
     * 根据id查询套餐信息
     *
     * @param id
     * @return
     */
    @Transactional
    public SetmealVO getById(Long id) {
        //获取套餐基本信息(套餐信息和分类名称)
        SetmealVO setmealVO = setmealMapper.getByIdWithCategoryName(id);
        //根据套餐id获取对应套餐关系信息
        List<SetmealDish> setmealDishes = setmealDishMapper.getDishesBySetmealId(id);

        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 启用禁用状态
     *
     * @param status
     * @param id
     */
    @Transactional
    public void startOrStop(Integer status, Long id) {
        //需要判断套餐中的菜品是否处于起售状态 若有停售则显示无法启售
        if (status.equals(StatusConstant.ENABLE)) {
            //查看套餐中对应的菜品id
            List<SetmealDish> setmealDishes = setmealDishMapper.getDishesBySetmealId(id);
            for (SetmealDish setmealDish : setmealDishes) {
                //根据套餐关系中的菜品id查询菜品是否起售
                if (dishMapper.getById(setmealDish.getDishId()).getStatus()
                        .equals(StatusConstant.DISABLE)) {
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }//TODO 若点击停售 应该还要扩展
        }
        //修改状态
        Setmeal setmeal = Setmeal.builder().
                id(id)
                .status(status).
                build();
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
